var entContainer = $(".page > .content > .ent-container");
var goodContainer = $(".page > .content > .good-container");
var orderContainer = $(".page > .content > .order-container");
var buttonContainer = $(".page > .bottom");

$(function() {
	mobileGlobal.sendRequest(__mobilectx + "/supplier/order/loadOrderDetailData.xhtml", {orderid : __orderId}, null, function(data, status) {
		if (data.status) {
			entContainer.children(".item:eq(0)").append(data.result.leader + "&nbsp;&nbsp;" + data.result.contactnumber);
			entContainer.children(".item:eq(1)").append(data.result.entname);
			entContainer.children(".item:eq(2)").append(data.result.address);
			
			for (var i = 0; i < data.result.ordergoods.length; i ++) {
				var ordergood = data.result.ordergoods[i];
				
				var html = "<div class='item'>" + 
							"<img src='" + ordergood.goodpic + "'>" + 
							"<div class='right'>" + 
								"<div class='base-info'>" + 
									"<div class='name'>" + ordergood.goodname + "</div>" + 
									"<div class='specs'>" + ordergood.goodspecs + "</div>" + 
									"<div class='price'>￥<span class='number'>" + ordergood.goodprice.toFixed(2) + "</span>元/" + ordergood.meteringunit + "</div>" + 
								"</div>" + 
								"<div class='buy-info'>" + 
									"<div class='number'>x" + ordergood.goodnumber + "</div>" + 
									"<div class='total'>￥<span class='amount'>" + ordergood.goodtotal.toFixed(2) + "</span>元</div>" + 
								"</div>" + 
							"</div>" + 
						   "</div>";
				goodContainer.append($(html));
			}
			
			orderContainer.children(".item:eq(0)").append(data.result.orderid);
			orderContainer.children(".item:eq(1)").children(".number").append(data.result.ordertotal.toFixed(2));
			orderContainer.children(".item:eq(2)").append(new Date(data.result.confirmtime).format("yyyy-MM-dd HH:mm:ss"));
			
			var button = null;
			switch (data.result.orderstate) {
				case 8:
					button = "<span class='button' onclick='confirmDistribute();'>确认发货</span>";
					break;
				case 9:
					button = "<span class='button' onclick='cancelOrder();'>取消订单</span>";
					break;
			}
			if (button) buttonContainer.append($(button));
			
			$(".page > .content").removeClass("hidden");
			$(".page > .bottom").removeClass("hidden");
			
			mobileGlobal.hideLoading();
		} else {
			mobileGlobal.hideLoading();
			mobileGlobal.failed("加载数据失败");
		}
	});
});

//返回按钮点击事件
function onBackClick() {
	mobileGlobal.backword();
}

//确认发货
function confirmDistribute() {
	mobileGlobal.openConfirmDialog("订单确定已发货？", function() {
		mobileGlobal.closeConfirmDialog();
		
		mobileGlobal.sendRequest(__mobilectx + "/supplier/order/confirmDistribute.xhtml", {orderid : __orderId}, null, 
			function(data, status) {
				mobileGlobal.hideLoading();
				if (data.status) {
					mobileGlobal.openMessageDialog("确认发货成功！", function() {
						mobileGlobal.closeMessageDialog();
						mobileGlobal.backword();
					});
				} else {
					mobileGlobal.openMessageDialog(data.result, function() {
						mobileGlobal.closeMessageDialog();
					});
				}
			}, 
			function(xhr, msg, e) {
				mobileGlobal.hideLoading();
				mobileGlobal.openMessageDialog("网络请求错误！", function() {
					mobileGlobal.closeMessageDialog();
				});
			}
		);
	});
}

//取消订单
function cancelOrder() {
	mobileGlobal.openConfirmDialog("确定取消订单？", function() {
		mobileGlobal.closeConfirmDialog();
		
		mobileGlobal.sendRequest(__mobilectx + "/supplier/order/cancelOrder.xhtml", {orderid : __orderId}, null, 
			function(data, status) {
				mobileGlobal.hideLoading();
				if (data.status) {
					mobileGlobal.openMessageDialog("取消订单成功！", function() {
						mobileGlobal.closeMessageDialog();
						mobileGlobal.backword();
					});
				} else {
					mobileGlobal.openMessageDialog(data.result, function() {
						mobileGlobal.closeMessageDialog();
					});
				}
			}, 
			function(xhr, msg, e) {
				mobileGlobal.hideLoading();
				mobileGlobal.openMessageDialog("网络请求错误！", function() {
					mobileGlobal.closeMessageDialog();
				});
			}
		);
	});
}